package crm.view.desktop.iFrame.bar;

import java.awt.AlphaComposite;
import java.awt.CardLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;

import crm.util.Resources;
import crm.view.desktop.iFrame.StudentFrame;

public class IFrameFunktBar extends JPopupMenu implements ActionListener, MouseListener {
	StudentFrame frame;
	float transparency = 0.5f;
	public IFrameFunktBar(StudentFrame frame) {
		addMouseListener(this);
		this.frame=frame;
		initGUI();
//		setUndecorated(true);
		pack();
		setPopupSize(200, 30);
	}
	private void initGUI() {
		setBorder(new BevelBorder(BevelBorder.RAISED));

		setLayout(new GridLayout(1,0));
		JButton but;

		but = new JButton(Resources.getWindowsIcon());
		but.setToolTipText("Wechsle zur windows-Ansicht");
		but.setBorderPainted(false);
		but.setActionCommand("windows");
		but.addActionListener(this);
		add(but);
		but = new JButton(Resources.getDesktopIcon());
		but.setToolTipText("Wechsle zur Desktop-Ansicht");
		but.setBorderPainted(false);
		but.setActionCommand("desktop");
		but.addActionListener(this);
		add(but);
		but = new JButton(Resources.getHistoryIcon());
		but.setToolTipText("Wechsle zur History");
		but.setBorderPainted(false);
		but.setActionCommand("history");
		but.addActionListener(this);
		add(but);
		but = new JButton(Resources.getTransparentIcon());
		but.setToolTipText("kann bis jetzt nichts");
		but.setBorderPainted(false);
		but.addActionListener(this);
		but.setActionCommand("transparent");
		add(but);
		but = new JButton(Resources.getLockIcon());
		but.setToolTipText("Sperrt den PC, sodass der Benutzer nichts mehr machen kann");
		but.setBorderPainted(false);
		but.setActionCommand("lock");
		but.addActionListener(this);
		add(but);
		but = new JButton(Resources.getVNCIcon());
		but.setToolTipText("VNC-Remote");
		but.setBorderPainted(false);
		but.setActionCommand("vnc");
		but.addActionListener(this);
		add(but);


	}
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g.create();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
		super.paint(g2);
		g2.dispose();
	}

	public void actionPerformed(ActionEvent ev) {
		String cmd = ev.getActionCommand();
		if (cmd.equals("lock") ){
			System.out.println("lock");
			frame.getStudent().lockScreen();
			JButton but = (JButton)ev.getSource();
			but.setActionCommand("unlock");
		}else if (cmd.equals("unlock") ){
			System.out.println("unlock");
			frame.getStudent().unlockScreen();
			JButton but = (JButton)ev.getSource();
			but.setActionCommand("lock");
		}else if (cmd.equals("transparent")){
//			setOpaque(true);
			frame.setOpaque(true);
			//TODO: Transparenz bei InternalFrame muss noch gemacht werden
		}else if(cmd.equals("vnc")){
			
			ProcessBuilder p = new ProcessBuilder(new String[]{"java" , "-jar", "vncviewer.jar", frame.getStudent().getIP() + ":0"});
			
			try {
				p.start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else if(cmd.equals("windows")){
			CardLayout layout = (CardLayout)frame.getContentPane().getLayout();
			layout.show(frame.getContentPane(), "Windows");
		}else if(cmd.equals("desktop")){
			CardLayout layout = (CardLayout)frame.getContentPane().getLayout();
			layout.show(frame.getContentPane(), "Desktop");
		}else if(cmd.equals("history")){
			CardLayout layout = (CardLayout)frame.getContentPane().getLayout();
			layout.show(frame.getContentPane(), "History");
		}
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
//		transparency = 1.0f;
		repaint();

	}
	@Override
	public void mouseExited(MouseEvent arg0) {

	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
